package com.zzedu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zzedu.common.core.domain.entity.SysUser;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.system.domain.SysConfig;
import com.zzedu.system.domain.SysNotice;
import com.zzedu.system.mapper.SysNoticeMapper;
import com.zzedu.system.service.ISysNoticeService;
import com.zzedu.system.service.ISysUserService;
import org.apache.commons.collections4.MapUtils;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 公告 服务层实现
 *
 * @author smarthome
 * @date 2018-06-25
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService {
    @Autowired
    ISysUserService sysUserService;

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public IPage<SysNotice> selectNoticeList(IPage<SysNotice> page, SysNotice notice) {
        LambdaQueryWrapper<SysNotice> queryWrapper = Wrappers.lambdaQuery(SysNotice.class);
        queryWrapper.like(StringUtils.isNotBlank(notice.getNoticeTitle()), SysNotice::getNoticeTitle, notice.getNoticeTitle());
        queryWrapper.eq(StringUtils.isNotBlank(notice.getStatus()), SysNotice::getStatus, notice.getStatus());
        queryWrapper.eq(StringUtils.isNotBlank(notice.getNoticeType()), SysNotice::getNoticeType, notice.getNoticeType());
        queryWrapper.eq(null != notice.getCreateBy(), SysNotice::getCreateBy, notice.getCreateBy());
        Map map = notice.getParams();
        if (!MapUtils.isEmpty(map)) {
            if (map.containsKey("beginTime")) {
                queryWrapper.ge(SysNotice::getGmtCreate, MapUtils.getString(map, "beginTime"));
            }
            if (map.containsKey("endTime")) {
                queryWrapper.le(SysNotice::getGmtCreate, MapUtils.getString(map, "endTime"));
            }
        }
        if(notice.getSysUser()!=null){
            SysUser sysUser = notice.getSysUser();
            if(StringUtils.isNotBlank(sysUser.getNickName())){
                queryWrapper.apply("create_by in(select id from sys_user where deleted=0 and nick_name like concat('%', {0},'%') )",sysUser.getNickName());
            }
        }
        queryWrapper.orderByDesc(SysNotice::getId);
        page.setRecords(Lists.newArrayList());
        this.baseMapper.selectList(page, queryWrapper, new ResultHandler<SysNotice>() {
            @Override
            public void handleResult(ResultContext<? extends SysNotice> resultContext) {
                SysNotice sysNotice = resultContext.getResultObject();
                if (null != sysNotice.getCreateBy()) {
                    sysNotice.setSysUser(sysUserService.getById(sysNotice.getCreateBy()));
                }
                page.getRecords().add(sysNotice);
            }
        });
        return page;
    }

}
